package com.wuwei.elearning.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wuwei.elearning.question.domain.SelectRecord;
import com.wuwei.elearning.question.domain.UserRecord;
import com.wuwei.elearning.question.mapper.SelectRecordMapper;
import com.wuwei.elearning.question.mapper.UserRecordMapper;
import com.wuwei.elearning.question.service.UserRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户做题记录表，每一个该表都对应一个用户做题记录 服务实现类
 * </p>
 *
 * @author wuwei
 * @since 2024-03-28
 */
@Service
public class UserRecordServiceImpl extends ServiceImpl<UserRecordMapper, UserRecord> implements UserRecordService {

    @Resource
    private UserRecordMapper userRecordMapper;

    @Resource
    private SelectRecordMapper selectRecordMapper;


    /**
     * 当用户退出顺序练习界面时，会向数据库更新一下用户本次练习到了哪一道题
     * @param index 表示用戶做到哪一題了
     * @param selectedId 如果小于10000，则为chapter表中的id字段，如果大于10000，则为subject表的id加上10000
     * @param account user表的account，用戶账号
     * @param rightNum 做对题目的数量
     * @param selectedNum 做的题目的总数
     */
    @Override
    public void addOrUpdate(Integer index, Long selectedId, Long account,Integer rightNum, Integer selectedNum) {
        LambdaQueryWrapper<UserRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRecord::getUserAccount,account)
                .eq(UserRecord::getSelectedId,selectedId);
        UserRecord userRecord = userRecordMapper.selectOne(queryWrapper);
        //这里要向数据库查询，这里要注意，当按照selectedId和user查询时，最多只能查到一条记录，因为一个用户在一个章节练习的时候只会留下一条记录，后续都是修改这一记录
        if (userRecord==null) {
            userRecord = new UserRecord();
            userRecord.setIndexRecord(index);
            userRecord.setSelectedId(selectedId);
            userRecord.setUserAccount(account);
            userRecord.setLastTime(LocalDateTime.now());
            userRecord.setRightNum(rightNum);
            userRecord.setSelectedNum(selectedNum);
            userRecordMapper.insert(userRecord);
        } else {
            userRecord.setIndexRecord(index);
            userRecord.setLastTime(LocalDateTime.now());
            userRecord.setRightNum(rightNum);
            userRecord.setSelectedNum(selectedNum);
            userRecordMapper.updateById(userRecord);
        }
    }

    /**
     * 删除用户做题的记录
     * 需要删除user_record表和select_record表
     * @param selectedId 如果小于10000，则为chapter表中的id字段，如果大于10000，则为subject表的id加上10000
     * @param account user表的account
     * @return
     */
    @Override
    @Transactional
    public void deleteBySelectId(Long selectedId, Long account) {
        //删除user_record表
        LambdaQueryWrapper<UserRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRecord::getSelectedId,selectedId).eq(UserRecord::getUserAccount,account);
        userRecordMapper.delete(queryWrapper);

        //删除select_record表
        LambdaQueryWrapper<SelectRecord> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SelectRecord::getSelectedId,selectedId).eq(SelectRecord::getUserAccount,account);
        selectRecordMapper.delete(queryWrapper1);
    }
}
